package rs.mbs2.epics.server.servlet;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import rs.mbs2.epics.server.entity.Album;
import rs.mbs2.epics.server.entity.Photo;
import rs.mbs2.epics.server.entity.Rating;
import rs.mbs2.epics.server.entity.User;
import rs.mbs2.epics.server.session.dao.localinterface.AlbumDaoLocal;
import rs.mbs2.epics.server.session.dao.localinterface.PhotoDaoLocal;
import rs.mbs2.epics.server.session.dao.localinterface.RatingDaoLocal;
import rs.mbs2.epics.server.session.dao.localinterface.UserDaoLocal;

@SuppressWarnings("serial")
public class ViewPhotoController extends HttpServlet {

	@EJB
	private PhotoDaoLocal photoDao;
	
	@EJB
	private UserDaoLocal userDao;
	
	@EJB
	private AlbumDaoLocal albumDao;
	
	@EJB
	private RatingDaoLocal ratingDao;
	
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("ViewPhotoController.doGet()");
		
		String photoIdString = request.getParameter("id");
		System.out.println(photoIdString);
		Integer photoId = Integer.parseInt(photoIdString);
		
		// ********** photoOwner **************
		Album album = null;
		List<Album> albums = albumDao.findAlbumsByPhotoId(photoId);
		if(albums != null && !albums.isEmpty()) {
			for(int i=0; i<albums.size(); i++)
			if(!albums.get(i).getTitle().equalsIgnoreCase("favorites")) {
				album = albums.get(i);
				break;
			}
		}
		
		if(album != null) {
			User photoOwner = userDao.findUserThatCreatedAlbum(album.getId());
			request.setAttribute("photoOwner", photoOwner);
		}
		
		// ********** photo **************
		Photo photo = photoDao.findById(photoId);
		request.setAttribute("photo", photo);

		System.out.println(photo.getTitle() + " - " + photo.getDescription());
		
		
		User userSession = (User) request.getSession().getAttribute("user");
		
		if(userSession != null) {
			User user = userDao.findById(userSession.getId());
			
			if(user.getFavouriteAlbum().getPhotos().contains(photo))
				request.setAttribute("favouritePhoto", true);
			else 
				request.setAttribute("favouritePhoto", false);
			
			// provera da li je korisnik vec ocenio sliku
			List<Rating> ocene = ratingDao.findRatingByPhotoAndUser(photoId, userSession.getId());
			if( !ocene.isEmpty()) {
				request.setAttribute("rated", true);
				request.setAttribute("rate", ocene.get(0).getVote());
			}
			else
				request.setAttribute("rated", false);
		}
		
		
		getServletContext().getRequestDispatcher("/viewPhoto.jsp").forward(request, response);
		return;
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
}
